﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace W.Device.Repository
{
    public class BaseRepositoy<T> : IRepository<T> where T : class, new()
    {
        private readonly EFDeviceDbContext db;

        public BaseRepositoy(EFDeviceDbContext db)
        {
            this.db = db;
        }

        public async Task<int> AddAsync(T entity)
        {
            await db.AddAsync(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<List<T>> GetAllAsaync()
        {
            return await db.Set<T>().ToListAsync();
        }

        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
        {
            return await db.Set<T>().Where(expre).ToListAsync();
        }

        public async Task<int> Update(T entity)
        {
           db.Update(entity);
           return await db.SaveChangesAsync();
        }

        public async Task<int> UpdateAll(List<T> entitys)
        {
            db.UpdateRange(entitys);
            return await db.SaveChangesAsync();
        }
    }
}
